// ==UserScript==
// @name         JS推送题目
// @namespace    http://tampermonkey.net/
// @version      0.1.20220516
// @description  仅用于学习使用，网课自动挂机看视频，网课作业自动做题共享答案。网课在线辅助脚本❤网课辅助题库❤全自动化操作挂机【易全题库】
// @author       YanMing
// @match        *://www.baidu.com/*
// @match        *://*.uooconline.com/*
// @icon         
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const SETTING = {
        api: "http://localhost:8080"
    }
    uooc_course_list();
    //uooc_course_id_catalog('1091609371')
    // 获取课程列表
    function uooc_course_list(){
        // 已学完的课程https://cce.org.uooconline.com/home/course/list?keyword=&page=1&type=finish
        setTimeout(() => {
            uooc_course_learn_type('finish')
        }, 1000);
        // 正在学的课程https://cce.org.uooconline.com/home/course/list?keyword=&page=1&type=learn
        // 延后执行
        setTimeout(() => {
            uooc_course_learn_type('learn')
        }, 3000);

    }

    let  pushCourseInfo = {course_id:'',course_name:'',org_name:'',teacher_name:'',chapter_id:'',chapter_name:'',section_id:'',subsection_id:'',subsection_name:'',catalog_id:'',task_id:''}
    function uooc_course_learn_type(type){
        if(type == "learn" || type == "finish" ){
            $.ajax({
                type: "GET",
                url: '/home/course/list',
                data: {
                    page: 1,
                    type: type,
                    keyword: ''
                },
                success: function (response) {
                    // 判断是否提交试卷
                    if (response.code == 1) {
                        if(response.data && response.data.data && response.data.data.length > 0) {
                            uooc_course(response.data.data)
                        }
                    }
                }
            });
        }

    }

    function uooc_course(courseData){
        if(courseData && courseData.length>0){
            for(let i=0;i<courseData.length;i++){
                let courseInfo = JSON.parse(JSON.stringify(pushCourseInfo))
                courseInfo.course_id = courseData[i].id;
                courseInfo.course_name = courseData[i].parent_name;
                courseInfo.org_name = courseData[i].org_name;
                courseInfo.teacher_name = courseData[i].uname;
                const course = {course_id: courseData[i].id,course_name: courseData[i].parent_name,org_name: courseData[i].org_name,teacher_name: courseData[i].uname}
                // 发送课程信息到服务器
                $.ajax({
                    type: "POST",
                    url: SETTING.api+'/uooc/course/push',
                    dateType: "json",
                    contentType: "application/json",
                    data: JSON.stringify(course),
                    success: function (response) {

                    }
                });
                // 获取章节目录
                uooc_course_catalog(courseInfo);
                // 测验 /home/task/testList?cid=xxxx&page=1&pagesize=20
                uooc_course_tests(courseInfo);
                // 作业 /home/task/homeworkList?cid=xxxx&page=1&pagesize=20
                uooc_course_works(courseInfo);
                // 考试（提交后不能查看） /home/task/examList?cid=2054062924


            }
        }
    }

    function uooc_course_catalog(course){
        var courseInfo = JSON.parse(JSON.stringify(course))
        // 查询目录是否存在
        $.ajax({
            type: "GET",
            url: SETTING.api+'/uooc/course/find?courseId='+courseInfo.course_id,
            dateType: "json",
            contentType: "application/json",
            success: function (response) {
                if(response.code == 200  && response.data && response.data.length > 0){
                    return;
                }else{
                    $.ajax({
                        type: "GET",
                        url: '/home/learn/getCatalogList',
                        data: {
                            cid: courseInfo.course_id
                        },
                        success: function (response) {
                            if (response.code == 1) {
                                // 发送课程章节信息到服务器
                                let data = JSON.stringify({courseId:courseInfo.course_id,courseCatalogList: response.data});
                                $.ajax({
                                    type: "POST",
                                    url: SETTING.api+'/uooc/course/catalog',
                                    dateType: "json",
                                    contentType: "application/json",
                                    data: data,
                                    success: function (response) {
                                        return;
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    }



    // 测验 /home/task/testList?cid=xxxx&page=1&pagesize=20
    function uooc_course_tests(course){
        console.log("正在同步["+course.course_name+"测验]题目！")
        $.ajax({
            type: "GET",
            url: '/home/task/testList',
            data: {
                cid: course.course_id,
                page: 1,
                pagesize: 500,
            },
            success: function (response) {
                if (response.code == 1 && response.data.data && response.data.data.length > 0) {
                    const courseInfo = JSON.parse(JSON.stringify(course))
                    for(const courseTest of response.data.data){
                        courseInfo.chapter_id = courseTest.chapter_id;
                        courseInfo.chapter_name = courseTest.chapter_name+" "+courseTest.chapter_text;
                        courseInfo.section_id =  courseTest.section_id;
                        courseInfo.section_name =   courseTest.section_text;
                        courseInfo.subsection_id =   courseTest.point_id;
                        courseInfo.subsection_name =  courseTest.point_text;
                        courseInfo.catalog_id =  courseTest.catalog_id
                        courseInfo.task_id = courseTest.id
                        uooc_course_chapter_task_exam(courseInfo)
                    }
                }
            }
        });
    }
    // 作业 /home/task/homeworkList?cid=xxxx&page=1&pagesize=20
    function uooc_course_works(course){
        console.log("正在同步["+course.course_name+"作业]题目！")
        $.ajax({
            type: "GET",
            url: '/home/task/homeworkList',
            data: {
                cid: course.course_id,
                page: 1,
                pagesize: 500,
            },
            success: function (response) {
                if (response.code == 1) {
                    if (response.code == 1 && response.data.data && response.data.data.length > 0) {
                        const courseInfo = JSON.parse(JSON.stringify(course))
                        for (const courseWork of response.data.data) {
                            courseInfo.chapter_id = '';
                            courseInfo.chapter_name = '';
                            courseInfo.section_id =  '';
                            courseInfo.section_name = '';
                            courseInfo.subsection_id =  '';
                            courseInfo.subsection_name = '';
                            courseInfo.catalog_id = '';
                            courseInfo.task_id = courseWork.id
                            uooc_course_chapter_task_exam(courseInfo)
                        }
                    }
                }
            }
        });
    }

    function uooc_course_chapter_task_exam(course){
        var courseExamInfo = JSON.parse(JSON.stringify(course))
        $.ajax({
            type: "GET",
            url: '/exam/view',
            data: {
                cid: courseExamInfo.course_id,
                tid: courseExamInfo.task_id
            },
            success: function (response) {
               if (response.code == 1) {
                   uooc_push_course_chapter_task_exam_questions (courseExamInfo,response.data)
                }
            }
        });

    }

    function uooc_push_course_chapter_task_exam_questions(course, data) {
        const courseInfo = JSON.parse(JSON.stringify(course))
        //console.log("同步==>"+courseInfo.course_name+"("+courseInfo.course_id+")->"+courseInfo.chapter_name+"("+courseInfo.chapter_id+")->"+courseInfo.section_name+"("+courseInfo.section_id+")->"+courseInfo.subsection_name+"("+courseInfo.subsection_id+")-> Task = "+courseInfo.task_id)
        // 提交试卷到服务器
        const examPushData = JSON.stringify( {course:courseInfo, exam:data});
        $.ajax({
            type: "POST",
            url: SETTING.api+'/uooc/exam/push',
            dateType: "json",
            contentType: "application/json",
            data: examPushData,
            success: function (response) {

            }
        });
    }


})();

